package com.czl.module_service.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.czl.module_service.R;

import java.util.List;

/**
 * @author by xubin,
 * @date on 2022/1/20.
 * PS: Not easy to write code, please indicate.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private final int mDividerHeight;
    private final int mHorizontalSpace;
    private final Paint mPaint;
    private List<Integer> skipPosition;

    public DividerItemDecoration(int dividerHeight, int color, int horizontalSpace) {
        mDividerHeight = dividerHeight;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(color);
        this.mHorizontalSpace = horizontalSpace;
    }

    public DividerItemDecoration(Context context, int horizontalSpace) {
        this(1, ContextCompat.getColor(context, R.color.color_F5F5F6), horizontalSpace);
    }

    public DividerItemDecoration(Context context, int dividerHeight, int horizontalSpace) {
        this(dividerHeight, ContextCompat.getColor(context, R.color.color_F5F5F6), horizontalSpace);
    }

    public DividerItemDecoration(Context context, int dividerHeight, int horizontalSpace, @ColorRes int id) {
        this(dividerHeight, ContextCompat.getColor(context, id), horizontalSpace);
    }

    public DividerItemDecoration(Context context, int dividerHeight, int horizontalSpace, @ColorRes int id, List<Integer> skipPosition) {
        this(dividerHeight, ContextCompat.getColor(context, id), horizontalSpace);
        this.skipPosition = skipPosition;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager && parent.getAdapter() != null) {
            if (((LinearLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.HORIZONTAL) {
                if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
                    outRect.set(0, 0, mDividerHeight, 0);
                }
            } else {
                if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
                    outRect.set(0, 0, 0, mDividerHeight);
                }
            }
        }

    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (manager instanceof LinearLayoutManager) {
            if (((LinearLayoutManager) manager).getOrientation() == LinearLayoutManager.VERTICAL) {
                drawVertical(c, parent);
            } else {
                drawHorizontal(c, parent);
            }
        }

    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        // 和drawVertical差不多 left right 与 top和bottom对调一下
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin +
                    Math.round(child.getTranslationX());
            final int right = left + mDividerHeight;
            c.drawRect(left, top, right, bottom, mPaint);
        }

    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        // recyclerView是否设置了paddingLeft和paddingRight
        final int left = parent.getPaddingLeft() + mHorizontalSpace;
        final int right = parent.getWidth() - parent.getPaddingRight() - mHorizontalSpace;
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (skipPosition != null) {
                if (skipPosition.contains(i)) {
                    continue;
                }
            }
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            // divider的top 应该是 item的bottom 加上 marginBottom 再加上 Y方向上的位移
            final int top = child.getBottom() + params.bottomMargin +
                    Math.round(child.getTranslationY());
            // divider的bottom就是top加上divider的高度了
            final int bottom = top + mDividerHeight;
            c.drawRect(left, top, right, bottom, mPaint);
        }
    }
}
